package com.raycloud.erp.omll.provider.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.raycloud.erp.mulplatforms.domain.*;
import com.raycloud.erp.omll.data.entity.dto.OmallTradeReqDTO;
import com.raycloud.erp.omll.data.entity.dto.TradeListQueryDTO;
import com.raycloud.erp.omll.data.entity.page.PageQuery;
import com.raycloud.erp.omll.data.entity.vo.OmallTradeResVO;
import com.raycloud.erp.omll.data.mapper.TradeMapper;
import com.raycloud.erp.omll.provider.constant.Constants;
import com.raycloud.erp.omll.provider.constant.OmallRefundStatus;
import com.raycloud.erp.omll.provider.constant.OmallTradeStatus;
import com.raycloud.erp.omll.provider.constant.TradeStatus;
import com.raycloud.erp.omll.provider.enums.RefundStatusEnum;
import com.raycloud.erp.omll.provider.enums.TradeStatusEnum;
import com.raycloud.erp.omll.provider.service.BatchTagShippedService;
import com.raycloud.erp.omll.provider.service.BusiTradeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/***
 * @DESCRIPTION: 订单交易dubbo接口实现
 * @author 枫伦
 * @params:
 * @return:
 * @Date: 2021/1/4 3:18 下午
 * @Modified By:
 */
@Service
@Slf4j
public class BusiTradeServiceImpl implements BusiTradeService {
    @Autowired
    private TradeMapper tradeMapper;
    @Autowired
    private OmallTradeStatus omallTradeStatus;
    @Autowired
    private BatchTagShippedService batchTagShippedService;

    /**
     * @param tradeReqBO
     * @DESCRIPTION: 查询单笔交易信息
     * @author 枫伦
     * @params: [tradeReqBO]
     * @return: com.raycloud.erp.mulplatforms.domain.TradeResDto
     * @Date: 2021/1/13 10:53 下午
     * @Modified By:
     */
    @Override
    public TradeResDto getSingerTrade(TradeReqDto tradeReqBO) {
        if (!tradeReqBO.getSource().contains(Constants.SOURCE) || !StringUtils.isNotBlank(tradeReqBO.getTradeId())
        ) {
            return null;
        }
        OmallTradeReqDTO omallTradeReqDTO = new OmallTradeReqDTO();
        BeanUtils.copyProperties(tradeReqBO, omallTradeReqDTO);
        OmallTradeResVO omallTradeResVO = tradeMapper.getSingerTrade(omallTradeReqDTO);
        TradeResDto tradeResDto = new TradeResDto();
        BeanUtils.copyProperties(tradeResDto, omallTradeResVO);
        return tradeResDto;
    }

    /***
     * @DESCRIPTION: 查询交易总数
     * @author 枫伦
     * @params: [companyId, userId]
     * @return: int
     * @Date: 2021/1/13 4:58 下午
     * @Modified By:
     * @param tradeReqBO
     */
    @Override
    public int getTotalCountTrade(TradeReqDto tradeReqBO) {
        if (Constants.SOURCE.equalsIgnoreCase(tradeReqBO.getSource())) {
            log.info("source名称有误:{}", tradeReqBO.getSource());
            return 0;
        }
        OmallTradeReqDTO omallTradeReqDTO = new OmallTradeReqDTO();
        BeanUtils.copyProperties(tradeReqBO, omallTradeReqDTO);
        return tradeMapper.getTotalCountTrade(omallTradeReqDTO);
    }

    /***
     * @DESCRIPTION: 单笔, 多笔交易发货
     * @author 枫伦
     * @params: [tradeConsignReqDto]
     * @return: java.lang.Boolean
     * @Date: 2021/1/14 9:43 上午
     * @Modified By:
     * @param tradeConsignReqDto
     */
    @Override
    public Boolean uploadTradeConsign(TradeConsignReqDto tradeConsignReqDto) {
        if (tradeConsignReqDto.getSource().equalsIgnoreCase(Constants.SOURCE)) {
            return false;
        }
        return batchTagShippedService.uploadTradeConsign(tradeConsignReqDto);
    }


    /***
     * @DESCRIPTION: 订单交易列表
     * @author 枫伦
     * @params: [tradeReqDto]
     * @return: com.raycloud.erp.mulplatforms.domain.TradeListResDto
     * @Date: 2021/1/14 2:38 下午
     * @Modified By:
     * @param tradeReqDto
     */
    @Override
    public TradeListResDto getListTrade(TradeReqDto tradeReqDto) {
        if (Constants.SOURCE.equalsIgnoreCase(tradeReqDto.getSource())) {
            if (Constants.SOURCE.equalsIgnoreCase(tradeReqDto.getSource())) {
                log.info("source名称有误:{}", tradeReqDto.getSource());
                return null;
            }
        }
        PageQuery<TradeListQueryDTO> pageQuery = new PageQuery<>();
        pageQuery.setPageNum(tradeReqDto.getPageNo().intValue());
        if (!Objects.isNull(tradeReqDto.getPageSize())) {
            pageQuery.setPageSize(tradeReqDto.getPageSize().intValue());
        }
        TradeListQueryDTO tradeListQueryDTO = new TradeListQueryDTO();
        tradeListQueryDTO.setCompanyId(tradeReqDto.getCompanyId())
                .setUserId(tradeReqDto.getUserId())
//                .setSource(tradeReqDto.getSource())
                .setStart_modified(tradeReqDto.getStartModified())
                .setEnd_modified(tradeReqDto.getEndModified())
                .setStatus(tradeReqDto.getStatus());
        pageQuery.setQueryData(tradeListQueryDTO);
        IPage<OmallTradeResVO> iPage = tradeMapper.tradePageList(pageQuery);
        //返回结果
        List<TradeResDto> tradeResDtoList = new ArrayList<>();
        iPage.getRecords().stream().forEach(bean -> {
            TradeResDto tradeResDto = new TradeResDto();
            BeanUtils.copyProperties(bean, tradeResDto);
            tradeResDtoList.add(tradeResDto);
        });
        TradeListResDto tradeListResDto = new TradeListResDto();
        tradeListResDto.setTotalResults(iPage.getTotal());
        tradeListResDto.setUseHasNext(iPage.getTotal() > pageQuery.getPageNum() * pageQuery.getPageSize() ? true : false);
        tradeListResDto.setTradeResDtoList(tradeResDtoList);
        return tradeListResDto;
    }

    /***
     * @DESCRIPTION: 查询平台支持的物流和ERP物流信息的映射关系
     * @author 枫伦
     * @params: [source]
     * @return: java.util.Map<com.raycloud.erp.mulplatforms.domain.LogisticsCompany, com.raycloud.erp.mulplatforms.domain.LogisticsCompany>
     * @Date: 2021/1/13 5:54 下午
     * @Modified By:
     * @param source
     */
    @Override
    public Map<LogisticsCompany, LogisticsCompany> getLogisticsCompanyMapping(String source) {
        if (Objects.isNull(source) || StringUtils.isEmpty(source)) {
            return new HashMap<>();
        }


        return null;
    }

    /***
     * @DESCRIPTION:查询平台订单状态和ERP系统状态映射关系
     * @author 枫伦
     * @params: [source]
     * @return: java.util.Map<java.lang.Enum, java.lang.String>
     * @Date: 2021/1/13 5:55 下午
     * @Modified By:
     * @param source
     */
    @Override
    public Map<String, String> getPlatformTradeStatusMapping(String source) {
        if (!source.equalsIgnoreCase(Constants.SOURCE)) {
            return null;
        }
        Map<String, String> enumStringMap = new HashMap<>();
        TradeStatusEnum[] values = TradeStatusEnum.values();
        Map<String, TradeStatus> map = omallTradeStatus.getMap();
        for (TradeStatusEnum value : values) {
            TradeStatus tradeStatus = map.get(value.status.toLowerCase());
            enumStringMap.put(value.status, tradeStatus.getSysStatus());
        }
        return enumStringMap;
    }

    /***
     * @DESCRIPTION:查询平台退款状态和ERP售后状态的映射关系查询
     * @author 枫伦
     * @params: [source]
     * @return: java.util.Map<java.lang.Enum, java.lang.String>
     * @Date: 2021/1/13 6:49 下午
     * @Modified By:
     * @param source
     */
    @Override
    public Map<String, String> getPlatformAfterSaleStatusMapping(String source) {
        if (!source.equalsIgnoreCase(Constants.SOURCE)) {
            return null;
        }
        Map<String, String> enumStringMap = new HashMap<>();
        RefundStatusEnum[] values = RefundStatusEnum.values();
        Map<String, String> map = OmallRefundStatus.refundStatusMap;
        for (RefundStatusEnum value : values) {
            enumStringMap.put(value.status, map.get(value.status.toLowerCase()));
        }
        return enumStringMap;
    }

    public static void main(String[] args) {
        for (Map.Entry<String, String> aa : get1111("omall").entrySet()) {
            System.out.println(aa.getKey() + "--->" + aa.getValue());
        }
    }

    public static Map<String, String> get1111(String source) {
        if (!source.equalsIgnoreCase(Constants.SOURCE)) {
            return null;
        }
        Map<String, String> enumStringMap = new HashMap<>();
        RefundStatusEnum[] values = RefundStatusEnum.values();
        Map<String, String> map = OmallRefundStatus.refundStatusMap;
        for (RefundStatusEnum value : values) {
            enumStringMap.put(value.status, map.get(value.status.toLowerCase()));
        }
        return enumStringMap;
    }
}
